const demo = {
  namespaced: true,
  state: {
    userInfo: {
      name: "qiankun",
      age: 15
    },
    //this.$store.state.demo.globalConfig.formSize
    globalConfig: { formSize: "small" },
    products: [
      {name: '鼠标', price: 20},
      {name: '键盘', price: 40},
      {name: '耳机', price: 60},
      {name: '显示屏', price: 80}
    ],
  },
  //绝对不允许出现异步
  mutations: {
    showFooter (state, payload ) {
      state.userInfo.age = payload
    },
    //this.$store.commit('minusPrice', 2); //提交`minusPrice,payload为2
    minusPrice (state, payload ) {
      let newPrice = state.products.forEach( product => {
        product.price -= payload
      })
    },
  },
  //回调函数的第一个参数是context, 是一个与store实例具有相同属性和方法的对象
  actions: {
    showFooterAsync( context, payload ) {
      setTimeout( () => {
        context.commit( 'showFooter', payload ); //context提交
      }, 2000)
    },
    //this.$store.dispatch('minusPriceAsync', 5); //分发actions中的minusPriceAsync这个异步函数
    minusPriceAsync( context, payload ) {
      setTimeout( () => {
        context.commit( 'minusPrice', payload ); //context提交
      }, 2000)
    }
  },
  //计算属性
  getters: {
    getToDo (state) {
      return state.globalConfig.formSize;
    },
    //this.$store.demo.getters.saleProducts
    saleProducts: (state) => {
      let saleProducts = state.products.map( product => {
        return {
          name: product.name,
          price: product.price / 2
        }
      })
      return saleProducts;
    }
  },
};
export default demo;